package com.heima.article.service.impl;

import com.heima.article.service.AppArticleInfoService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.crawler.core.parse.ZipUtils;
import com.heima.model.mappers.app.*;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.utils.common.BurstUtils;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AppArticleInfoServiceImpl implements AppArticleInfoService {

    @Autowired
    ApArticleContentMapper apArticleContentMapper;
    @Autowired
    ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;
    @Autowired
    private ApCollectionMapper apCollectionMapper;
    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;
    @Autowired
    private ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApAuthorMapper apAuthorMapper;

    public ResponseResult getArticleInfo(Integer articleId) {
        // 参数无效
        if (articleId == null || articleId < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticleConfig config = apArticleConfigMapper.selectByArticleId(articleId);
        Map<String, Object> data = new HashMap<>();
        // 参数无效
        if (config == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            //判断文章是否删除
        } else if (!config.getIsDelete()) {
            // 没删除的标识才返回给客户端
            ApArticleContent content = apArticleContentMapper.selectByArticleId(articleId);
            String unzip = ZipUtils.gunzip(content.getContent());
            content.setContent(unzip);
            data.put("content", content);
        }
        data.put("config", config);
        return ResponseResult.okResult(data);
    }

    /**
     * 加载文章详情的初始化配置信息，比如关注、喜欢、不喜欢、阅读位置等
     * @param dto
     * @return
     */
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (null == user && dto.getEquipmentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
             userId = user.getId();
        }
        //1，通过equipmentId或用户id查看行为实体--->entryId
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipemntId(userId, dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer entryId = apBehaviorEntry.getEntryId();
        Integer objectId = apBehaviorEntry.getId();
        Integer articleId = dto.getArticleId();
        Integer authorId = dto.getAuthorId();
        boolean  isCollection = false,isLike = false,isUnLike = false, isFollow = false;
        String burst = BurstUtils.groudOne(apBehaviorEntry.getId());
//        2，通过entryId和articleId去查看收藏表，看是否有数据，有数据就说明已经收藏，否则就是没有收藏
        ApCollection apCollection = apCollectionMapper.selectForEntryId(burst, objectId, entryId, ApCollection.Type.ARTICLE.getCode());
        if (apCollection != null) {
            isCollection = true;
        }
//        3，通过entryId和articleId去查看点赞表，看是否有数据，有数据就说明已经点赞，否则就是没有点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorMapper.selectLastLike(burst, objectId, entryId, ApLikesBehavior.Type.ARTICLE.getCode());
        if (apLikesBehavior != null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()) {
            isLike = true;
        }
//        4，通过entryId和articleId去查看不喜欢表，看是否有数据，有数据就说明不喜欢，否则就是喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectLastUnLike(entryId, articleId);
        if (apUnlikesBehavior != null && apUnlikesBehavior.getType()==ApUnlikesBehavior.Type.UNLIKE.getCode()) {
            isUnLike = true;
        }
//        5，通过authorId去app的id userId（app账号信息id）查看关注表，根据当前登录人的userId与作者的app账号id去查询，如果有数据，就说明已经关注，没有则说明没有关注I
        ApAuthor apAuthor = apAuthorMapper.selectById(authorId);
        if (user != null && apAuthor != null && apAuthor.getUserId() != null) {

            ApUserFollow apUserFollow = apUserFollowMapper.selectByFollowId(BurstUtils.groudOne(userId), userId, apAuthor.getUserId());
            if (apUserFollow != null) {
                isFollow = true;
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("isCollection", isCollection);
        data.put("isLike", isLike);
        data.put("isUnLike", isUnLike);
        data.put("isFollow", isFollow);
        return ResponseResult.okResult(data);
    }


}
